﻿using System.ComponentModel;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Jack.DataScience.Http.Jwt;
using Vav.Api.Utils;
using Vav.Api.GenericDtos;
using Vav.Data.TransferTypes;
using Newtonsoft.Json;
using Vav.Data.Models;
using Jack.DataScience.Data.Converters;
using Vav.Data.DataAccess;
using System;
using System.Collections.Generic;
using Vav.Data.Utils;
using Vav.Api.Authorization;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

namespace Vav.Api
{
    [Route("api/account")]
    public class AccounntController : BaseController
    {
        private readonly AccountDataAccess accountDataAccess;
        private readonly AuthOptions authOptions;
        private readonly RoleJwtEncoder roleJwtEncoder;
        private readonly GenericJwtToken genericJwtToken;
        public AccounntController(
            AccountDataAccess accountDataAccess,
            AuthOptions authOptions,
            GenericJwtToken genericJwtToken,
            RoleJwtEncoder roleJwtEncoder)
        {
            this.accountDataAccess = accountDataAccess;
            this.authOptions = authOptions;
            this.roleJwtEncoder = roleJwtEncoder;
            this.genericJwtToken = genericJwtToken;
        }

       
        [HttpPost, Description("小程序登陆以及注册"), Route("wxlogin")]
        public async Task<ActionResult<ApiBaseModel>> WxLogin([FromBody]GetWeChatProUserInfoRequest req)
        {
            var user = await accountDataAccess.WxLogin(req.code, req.appid, req.iv, req.encryptedData);
            if (user != null)
            {
                var token = new GenericJwtToken
                {
                    Valid = true,
                    UserId = user.Id,
                    ExpiringDate = DateTime.Now.AddDays(authOptions.TokenExpiringDays),
                    NickName = user.NickName,
                    Phone = user.Phone,
                    HeadPic = user.HeadPic,
                };
                if (user.State == 0)
                {
                    return ApiFail("您的账号已被管理员封禁，禁止登录");
                }
                token.Token = roleJwtEncoder.Encode(token);
                Response.WriteJWTCookie(token);
                return ApiSuccess(token);
            }
            return ApiFail("微信登录失败");
        }

        [HttpPost, Description("小程序登陆以及注册"), Route("qqlogin")]
        public async Task<ActionResult<ApiBaseModel>> QQLogin([FromBody]GetWeChatProUserInfoRequest req)
        {
            var user = await accountDataAccess.QQLogin(req.code, req.appid, req.iv, req.encryptedData);
            if (user != null)
            {
                var token = new GenericJwtToken
                {
                    Valid = true,
                    UserId = user.Id,
                    ExpiringDate = DateTime.Now.AddDays(authOptions.TokenExpiringDays),
                    NickName = user.NickName,
                    Phone = user.Phone,
                    HeadPic = user.HeadPic,
                };
                if (user.State == 0)
                {
                    return ApiFail("您的账号已被管理员封禁，禁止登录");
                }
                token.Token = roleJwtEncoder.Encode(token);
                Response.WriteJWTCookie(token);
                return ApiSuccess(token);
            }
            return ApiFail("微信登录失败");
        }

    }
}
